//
//  QJView.m
//  svqj
//
//  Created by xftt on 14-6-24.
//
//

#import "QJView.h"
#import "UIImageView+AFNetworking.h"

@implementation QJView
{
    BOOL isQR;
    BOOL isShare;
    BOOL _isEdit;
    UILabel *lbQR;
    UIImageView *iconQR;
    UILabel *lbShare;
    UIImageView *iconShare;
    UIImageView *icon;
    UIButton *btnWeixin;
    UIButton *btnPengYou;
    UIButton *btnQQ;
    UIButton *btnWeiBo;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    [self setBackgroundColor:[UIColor colorWithHexString:@"#5A5A5A"]];
    UIView *backImage = [[UIView alloc] initWithFrame:CGRectMake(8, 8, 220, 149)];
    [backImage setBackgroundColor:[UIColor colorWithHexString:@"#444444"]];
    [self addSubview:backImage];
    icon = [[UIImageView alloc] initWithFrame:CGRectMake(1, 1, 1, 1)];
    [backImage addSubview:icon];
    self.imgvQJ = [[UIImageView alloc] initWithFrame:CGRectMake(1, 1, 218, 147)];
    [self.imgvQJ setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    [backImage addSubview:self.imgvQJ];
    [self.imgvQJ setUserInteractionEnabled:YES];
    _title = [[UILabel alloc]initWithFrame:CGRectMake(8, 167, 220, 21)];
    [_title setBackgroundColor:[UIColor clearColor]];
    [_title setTextAlignment:NSTextAlignmentCenter];
    [_title setFont:[UIFont systemFontOfSize:15]];
    [_title setTextColor:[UIColor colorWithHexString:@"#AAAAAA"]];
    [self addSubview:_title];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(8, 195, 220, 1)];
    [line setBackgroundColor:[UIColor colorWithHexString:@"#777777"]];
    [self addSubview:line];
    _vQR = [[UIImageView alloc] initWithFrame:CGRectMake(1, 1, 218, 147)];
    [_vQR setBackgroundColor:[UIColor colorWithHexString:@"#444444"]];
    [_vQR setAlpha:0.7];
    [backImage addSubview:_vQR];
    [_vQR setHidden:!isQR];
    [_vQR setUserInteractionEnabled:YES];
    _imgvQR = [[UIImageView alloc] initWithFrame:CGRectMake(60, 23, 120, 120)];
    [_imgvQR setHidden:YES];
    [self addSubview:_imgvQR];
    _vShare = [[UIImageView alloc] initWithFrame:CGRectMake(1, 1, 218, 147)];
    [backImage addSubview:_vShare];
    [_vShare setBackgroundColor:[UIColor colorWithHexString:@"#444444"]];
    [_vShare setAlpha:0.7];
    [_vShare setHidden:!isShare];
    [_vShare setUserInteractionEnabled:YES];
    btnWeixin =  [[UIButton alloc] initWithFrame:CGRectMake(30, 103, 44, 44)];
    [btnWeixin setBackgroundImage:[UIImage imageNamed:@"weixin"] forState:UIControlStateNormal];
    [btnWeixin addTarget:self action:@selector(doShare:) forControlEvents:UIControlEventTouchUpInside];
    [btnWeixin setTag:1];
    [btnWeixin setHidden:YES];
    [self addSubview:btnWeixin];
    btnPengYou =  [[UIButton alloc] initWithFrame:CGRectMake(98, 103, 44, 44)];
    [btnPengYou setBackgroundImage:[UIImage imageNamed:@"pengyou"] forState:UIControlStateNormal];
    [btnPengYou addTarget:self action:@selector(doShare:) forControlEvents:UIControlEventTouchUpInside];
    [btnPengYou setTag:2];
    [btnPengYou setHidden:YES];
    [self addSubview:btnPengYou];
    btnWeiBo =  [[UIButton alloc] initWithFrame:CGRectMake(165, 103, 44, 44)];
    [btnWeiBo setBackgroundImage:[UIImage imageNamed:@"xinlang"] forState:UIControlStateNormal];
    [btnWeiBo addTarget:self action:@selector(doShare:) forControlEvents:UIControlEventTouchUpInside];
    [btnWeiBo setTag:4];
    [btnWeiBo setHidden:YES];
    [self addSubview:btnWeiBo];
    UITapGestureRecognizer * reg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doClick:)];
    UIView *vbutton1 = [[UIView alloc] initWithFrame:CGRectMake(8, 206, 100, 35)];
    [vbutton1 setBackgroundColor:[UIColor colorWithHexString:@"#444444"]];
    [vbutton1 setUserInteractionEnabled:YES];
    [vbutton1 setTag:1];
    [vbutton1 addGestureRecognizer:reg];
    iconQR = [[UIImageView alloc] initWithFrame:CGRectMake(8, 9, 19, 16)];
    [iconQR setImage:[UIImage imageNamed:@"Travel"]];
    [vbutton1 addSubview:iconQR];
    lbQR = [[UILabel alloc] initWithFrame:CGRectMake(29, 0, 71, 35)];
    [lbQR setFont:[UIFont systemFontOfSize:12]];
    [lbQR setBackgroundColor:[UIColor clearColor]];
    [lbQR setTextColor:[UIColor colorWithHexString:@"#BBBBBB"]];
    [lbQR setText:@"穿越到手机"];
    [vbutton1 addSubview:lbQR];
    [self addSubview:vbutton1];
    UITapGestureRecognizer * reg2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doClick:)];
    UIView *vbutton2 = [[UIView alloc] initWithFrame:CGRectMake(128, 206, 100, 35)];
    [vbutton2 setBackgroundColor:[UIColor colorWithHexString:@"#444444"]];
    [vbutton2 setUserInteractionEnabled:YES];
    [vbutton2 setTag:2];
    [vbutton2 addGestureRecognizer:reg2];
    iconShare = [[UIImageView alloc] initWithFrame:CGRectMake(8, 9, 19, 16)];
    [iconShare setImage:[UIImage imageNamed:@"Share"]];
    [vbutton2 addSubview:iconShare];
    lbShare = [[UILabel alloc] initWithFrame:CGRectMake(29, 0, 71, 35)];
    [lbShare setFont:[UIFont systemFontOfSize:12]];
    [lbShare setBackgroundColor:[UIColor clearColor]];
    [lbShare setTextColor:[UIColor colorWithHexString:@"#BBBBBB"]];
    [lbShare setText:@"分享/推广"];
    [vbutton2 addSubview:lbShare];
    [self addSubview:vbutton2];
    return self;
}

-(void)setViewEdit:(BOOL)isEdit
{
    _isEdit = isEdit;
    if (isEdit) {
        if ([self.quanjing.sortname length]>0) {
        NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
        NSArray* _QJSort = [_Data objectForKey:@"QJSort"];
        for (id sort in _QJSort) {
            if ([[sort objectForKey:@"sortid"] isEqualToString:self.quanjing.sortid]) {
                [lbQR setText:[sort objectForKey:@"sortname"]];
            }
        }
        }else{
        [lbQR setText:@"移动至分类"];}
        [iconQR setImage:[UIImage imageNamed:@"Move"]];
        [lbShare setText:@"删除全景"];
        [iconShare setImage:[UIImage imageNamed:@"Delete"]];
    }else{
    [lbQR setText:@"穿越到手机"];
    [iconQR setImage:[UIImage imageNamed:@"Travel"]];
    [lbShare setText:@"分享/推广"];
    [iconShare setImage:[UIImage imageNamed:@"Share"]];
    }
}

- (void)doClick:(UITapGestureRecognizer*)sender
{
    if (sender.view.tag == 1) {
        if (_isEdit) {
        [self.FController selectSortMenu:self.quanjing.dic];
        }else{
        isQR = !isQR;
        [_vShare setHidden:YES];
        [btnWeixin setHidden:YES];
        [btnPengYou setHidden:YES];
        [btnQQ setHidden:YES];
        [btnWeiBo setHidden:YES];
        [_vQR setHidden:!isQR];
        [_imgvQR setHidden:!isQR];
        isShare = NO;
        }
    }else if(sender.view.tag == 2){
        if (_isEdit) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确认删除全景？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除",nil];
        [alert show];
        }else{
        isShare = !isShare;
        [_vQR setHidden:YES];
        [_imgvQR setHidden:YES];
        [btnWeixin setHidden:!isShare];
        [btnPengYou setHidden:!isShare];
        [btnQQ setHidden:!isShare];
        [btnWeiBo setHidden:!isShare];
        [_vShare setHidden:!isShare];
        isQR = NO;
        }
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSUserDefaults *_Data = [NSUserDefaults standardUserDefaults];
        NSMutableArray *_QJData = [NSMutableArray arrayWithArray:[_Data objectForKey:@"QJData"]];
        for (int i=0; i<[_QJData count]; i++) {
            NSDictionary *qjdictionary = [_QJData objectAtIndex:i];
            if ([[qjdictionary objectForKey:@"sid" ] isEqualToString:_quanjing.sid]) {
                [_QJData removeObject:qjdictionary];
                break;
            }
        }
        [_Data setObject:_QJData forKey:@"QJData"];
        NSMutableArray *_QJTemp = [NSMutableArray arrayWithArray:[_Data objectForKey:@"QJTemp"]];
        for (int i=0; i<[_QJTemp count]; i++) {
        NSDictionary *qjdictionary = [_QJTemp objectAtIndex:i];
        if ([[qjdictionary objectForKey:@"sid" ] isEqualToString:_quanjing.sid]) {
            [_QJTemp removeObject:qjdictionary];
            break;
        }}
        [_Data setObject:_QJTemp forKey:@"QJTemp"];
        [_Data synchronize];
        NSString* docPath = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];
        NSString* filePath = [NSString stringWithFormat: @"%@/SVQJ/%@", docPath, _quanjing.sid];
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
        [self.FController reloadList];
    }
}

- (void)doShare:(UIButton*)sender
{
    UIImage *imgShare = nil;
    switch (sender.tag) {
        case 1:{
        WXMediaMessage *message = [WXMediaMessage message];
        message.title = _quanjing.sharetitle;
        message.description = _quanjing.intro;
        imgShare = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_quanjing.icon]]];
        [message setThumbImage:imgShare];
        WXWebpageObject *ext = [WXWebpageObject object];
        ext.webpageUrl = _quanjing.mob;
        message.mediaObject = ext;
        SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.message = message;
        req.scene = WXSceneSession;
        [WXApi sendReq:req];
        break;
        }case 2:{
        WXMediaMessage *message = [WXMediaMessage message];
        message.title = _quanjing.sharetitle;
        message.description = _quanjing.intro;
        imgShare = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_quanjing.icon]]];
        [message setThumbImage:imgShare];
        WXWebpageObject *ext = [WXWebpageObject object];
        ext.webpageUrl = _quanjing.mob;
        message.mediaObject = ext;
        SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.message = message;
        req.scene = WXSceneTimeline;
        [WXApi sendReq:req];
        break;
        }case 4:{
        WBMessageObject *message = [WBMessageObject message];
        WBWebpageObject *webpage = [WBWebpageObject object];
        webpage.objectID = @"identifier1";
        webpage.title = _quanjing.sharetitle;
        webpage.description = _quanjing.intro;
        NSString* docPath = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];
        NSString* imagePath = [NSString stringWithFormat: @"%@/SVQJ/%@/qjicon.png", docPath, _quanjing.sid];
        UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]];
        webpage.thumbnailData = UIImageJPEGRepresentation(img, 0.3);
        webpage.webpageUrl = _quanjing.pc;
        message.mediaObject = webpage;
        WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message];
        request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
                             @"Other_Info_1": [NSNumber numberWithInt:123],
                             @"Other_Info_2": @[@"obj1", @"obj2"],
                             @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
        [WeiboSDK sendRequest:request];
        break;
        }
    }
}



-(void)setQuanjing:(QuanJing *)quanjing
{
    NSString* docPath = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];
    NSString* imagePath = [NSString stringWithFormat: @"%@/SVQJ/%@/qjicon.png", docPath, quanjing.sid];
    NSString* qrPath = [NSString stringWithFormat: @"%@/SVQJ/%@/qricon.png", docPath, quanjing.sid];
    NSString* iconPath = [NSString stringWithFormat: @"%@/SVQJ/%@/icon.png", docPath, quanjing.sid];
    _quanjing = quanjing;
    [self.imgvQJ setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@#",quanjing.thumbnail]] BufferImagePath:imagePath];
    [_title setText:quanjing.title];
    [_imgvQR setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@#",quanjing.qrcode]] BufferImagePath:qrPath];
    [icon setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@#",quanjing.icon]] BufferImagePath:iconPath];
}


@end
